package com.szsh.aiot.hsm.controller;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.szsh.aiot.common.annotation.CustomPermission;
import com.szsh.aiot.common.annotation.Log;
import com.szsh.aiot.common.core.controller.BaseController;
import com.szsh.aiot.common.core.domain.AjaxResult;
import com.szsh.aiot.common.core.domain.RequestDto;
import com.szsh.aiot.common.enums.BusinessType;
import com.szsh.aiot.hsm.service.DeviceFlowService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 智能设备流量统计
 */
@RequestMapping(path = "/deviceFlow")
public class DeviceFlowController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(DeviceFlowController.class);

    private final DeviceFlowService deviceFlowService;

    private final Gson gson;

    public DeviceFlowController(DeviceFlowService deviceFlowService, Gson gson) {
        this.deviceFlowService = deviceFlowService;
        this.gson = gson;
    }

    /**
     * 智能设备流量统计所有设备查询接口
     * @param requestDto 请求参数
     * @return 处理结果
     */
    @RequestMapping(value = "/getAllDeviceList")
    @Log(title = "智能设备流量统计所有设备查询接口", businessType = BusinessType.SELECT)
    @CustomPermission
    @ResponseBody
    public AjaxResult getAllDeviceList(@RequestBody RequestDto requestDto) {
        return deviceFlowService.getAllDeviceList(requestDto);
    }

    /**
     * 智能设备流量统计已选择设备查询接口
     * @param requestDto 请求参数
     * @return 处理结果
     */
    @RequestMapping(value = "/getChoseDeviceList")
    @Log(title = "智能设备流量统计已选择设备查询接口", businessType = BusinessType.SELECT)
    @CustomPermission
    @ResponseBody
    public AjaxResult getChoseDeviceList(@RequestBody RequestDto requestDto) {
        return deviceFlowService.getChoseDeviceList(requestDto);
    }

    /**
     * 智能设备流量统计已选择设备保存接口
     * @param requestDto 请求参数
     * @return 处理结果
     */
    @RequestMapping(value = "/saveChoseDeviceList")
    @Log(title = "智能设备流量统计已选择设备保存接口", businessType = BusinessType.SELECT)
    @CustomPermission
    @ResponseBody
    public AjaxResult saveChoseDeviceList(@RequestBody RequestDto requestDto) {
        // 验证参数
        JsonObject requestParams = requestDto.getRequestJsonElement().getAsJsonObject();

        // 参数验证
        // deviceIdList
        if (!requestParams.has("deviceIdList")) {
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error("[deviceIdList缺失],requestParams:[" + requestParams + "]");
            }
            return AjaxResult.badRequest("deviceIdList缺失");
        }
        JsonElement jsonElement = requestParams.get("deviceIdList");
        if (!jsonElement.isJsonArray()) {
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error("[deviceIdList格式错误],deviceIdList:[" + jsonElement + "]");
            }
            return AjaxResult.badRequest("deviceIdList格式错误");
        }

        try {
            gson.fromJson(jsonElement, new TypeToken<List<Long>>(){}.getType());
        } catch (Exception e) {
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error("[deviceIdList数据错误],deviceIdList:[" + jsonElement + "]");
            }
            return AjaxResult.badRequest("deviceIdList数据错误");
        }

        return deviceFlowService.saveChoseDeviceList(requestDto);
    }

    /**
     * 监控设备流量获取接口
     * @param requestDto 请求参数
     * @return 处理结果
     */
    @RequestMapping(value = "/pictureData")
    @Log(title = "智能设备流量统计设备流量获取接口", businessType = BusinessType.SELECT)
    @CustomPermission
    @ResponseBody
    public AjaxResult pictureData(@RequestBody RequestDto requestDto) {
        // 验证参数
        JsonObject requestParams = requestDto.getRequestJsonElement().getAsJsonObject();

        // 参数验证
        // deviceId
        if (!requestParams.has("deviceId")) {
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error("[deviceId缺失],requestParams:[" + requestParams + "]");
            }
            return AjaxResult.badRequest("deviceId缺失");
        }
        JsonElement jsonElement = requestParams.get("deviceId");
        if (!jsonElement.isJsonPrimitive()) {
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error("[deviceId格式错误],deviceId:[" + jsonElement + "]");
            }
            return AjaxResult.badRequest("deviceId格式错误");
        }

        try {
            jsonElement.getAsLong();
        } catch (Exception e) {
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error("[deviceId数据错误],deviceId:[" + jsonElement + "]");
            }
            return AjaxResult.badRequest("deviceId数据错误");
        }

        return deviceFlowService.pictureData(requestDto);
    }
}
